package com.settlement.system.controller;

import com.settlement.system.common.enums.ReturnStatusEnum;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.common.annotation.LoggerProfile;
import com.settlement.system.model.entity.common.TblRrBillType;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblRrBillTypeService;
import com.settlement.system.common.util.common.FasUtil;
import com.alibaba.excel.context.AnalysisContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * RR-账单类型 前端控制器
 * @author xiaochuan
 * @since 2022-12-12
 */
@Slf4j
@Api(tags = "RR-账单类型",description = "RR-账单类型")
@RestController
@RequestMapping("/rr/billType")
public class TblRrBillTypeController extends ExcelController<TblRrBillType> {

    @Resource
    private ITblRrBillTypeService tblRrBillTypeService;

    @Override
    public IBaseService init() {
        return tblRrBillTypeService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblRrBillType.class;
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        FasUtil.formatInQueryCondition(model, "billCategory","code");
        super.setParamsModel(model);
    }

    @Override
    public String verifyImportObject(TblRrBillType tblRrBillType, AnalysisContext analysisContext) {
        String message = super.verifyImportObject(tblRrBillType,analysisContext);
        if(StringUtils.isNotEmpty(message)){
            return message;
        }
        try {
            Map<String,Object> map = new HashMap<>();
            map.put("billType",tblRrBillType.getBillType());
            List<TblRrBillType> tblRrVoucherTypes = tblRrBillTypeService.selectByParam(map);
            if(!CollectionUtils.isEmpty(tblRrVoucherTypes)){
                return "[账单类型]不允许重复";
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            return "校验时查询出错";
        }

        return null;
    }

    @Override
    protected String validateAllDate(List<TblRrBillType> datas) {
        List<String> list = new ArrayList<>();

        list = new ArrayList<>();
        for(TblRrBillType data : datas){
            String toString = data.getBillType();
            if(list.contains(toString)){
                return "有两行的账单类型数据重复";
            }else{
                list.add(toString);
            }
        }

        return super.validateAllDate(datas);
    }

    @Override
    protected void saveData(List<TblRrBillType> datas) throws ServiceException {
        super.saveData(datas);
        tblRrBillTypeService.updateCode();
    }

    @Override
    protected ObjectDataResponse insert(@RequestBody TblRrBillType model, HttpServletRequest request) throws ServiceException {
        String message = verifyImportObject(model,null);
        if (StringUtils.isNotEmpty(message)) {
            return new ObjectDataResponse(ReturnStatusEnum.CHECK_FAILURE.getValue(), message);
        }
        setDefualt(model);
        tblRrBillTypeService.insert(model);
        tblRrBillTypeService.updateCode();
        return new ObjectDataResponse(200, "新增成功");
    }

    @ApiOperation(value = "check")
    @LoggerProfile(methodNote="check")
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    public ObjectDataResponse check(@RequestBody TblRrBillType model) {
        String message = verifyImportObject(model,null);
        if(StringUtils.isNotEmpty(message)){
            return new ObjectDataResponse(ReturnStatusEnum.CHECK_FAILURE.getValue(), message);
        }
        return new ObjectDataResponse(200, "success");
    }

    @ApiOperation(value = "batchDisable")
    @LoggerProfile(methodNote="batchDisable")
    @RequestMapping(value = "/batchDisable", method = RequestMethod.POST)
    protected ObjectDataResponse batchDisable(@RequestBody List<TblRrBillType> disableList, HttpServletRequest request) throws ServiceException {
        String message = checkDisableList(disableList);
        if (StringUtils.isNotEmpty(message)) {
            return new ObjectDataResponse(ReturnStatusEnum.CHECK_FAILURE.getValue(), message);
        }
        tblRrBillTypeService.batchDisable(disableList);
        return new ObjectDataResponse(200, "success");
    }

    String checkDisableList(List<TblRrBillType> deleteList) throws ServiceException {

        return null;
    }
}

